package com.gitee.hljdrl.dialog.visitor;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.StyleRes;

import com.gitee.hljdrl.dialog.R;
import com.gitee.hljdrl.dialog.UIDialog;


public class PromptEditVisitor extends BasisViewPartVisitor implements View.OnClickListener {

    private String mInputText;

    public PromptEditVisitor(Context context) {
        super(context);
        setLayout(R.layout.dialog_prompt_edit_top_part);
        setDialogTheme(R.style.UIDialogEditText);
    }

    public PromptEditVisitor(Context context, @StyleRes int theme, @LayoutRes int layoutId) {
        super(context);
        setLayout(layoutId);
        setDialogTheme(theme);
    }

    public String getInputText() {
        return mInputText;
    }

    @Override
    protected void onContentTextView(TextView textView, CharSequence contentText) {
        if (contentText != null) {
            textView.setText(contentText);
        }
    }

    @Override
    public void onShow() {
        super.onShow();
        if (mEditText != null) {
            mEditText.requestFocus();
            mEditText.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // 显示软键盘
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);

                }
            }, 240);

        }
    }

    @Override
    public boolean onDismiss(Dialog dialog) {
        if(mEditText!=null){
            // 强制隐藏软键盘
            InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
            }
        }
        return super.onDismiss(dialog);
    }

    protected EditText mEditText;

    @Override
    public void onViewVisit(Dialog dialog, View view) {
        super.onViewVisit(dialog, view);
        mEditText = view.findViewById(R.id.dialog_tv_content);
        TextView tv_input_size = view.findViewById(R.id.tv_input_size);
        if(mEditText!=null){
            // 设置 TextWatcher
            int inputMax = 300;
            // 获取 EditText 的 InputFilter 数组
            InputFilter[] filters = mEditText.getFilters();
            // 遍历 filters，查找 LengthFilter
            for (InputFilter filter : filters) {
                if (filter instanceof InputFilter.LengthFilter) {
                    InputFilter.LengthFilter lengthFilter = (InputFilter.LengthFilter) filter;
                    // 获取最大长度
                    inputMax = lengthFilter.getMax();
                    Log.d("MaxLength", "The max length is: " + inputMax);
                    break;
                }
            }
            final int setMax = inputMax;
            mEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
                    // 文字变化之前的操作
                    Log.d("TextWatcher", "Before text changed: " + charSequence);
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                    // 文字变化时的操作
                    Log.d("TextWatcher", "On text changed: " + charSequence);
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    // 文字变化后的操作
                    Log.d("TextWatcher", "After text changed: " + editable.toString());
                    mInputText = editable.toString();
                    if (tv_input_size != null) {
                        tv_input_size.setText(editable.toString().length() + "/" + setMax);
                    }
                }
            });
        }
    }

    @Override
    protected Dialog createDialog(Context context, int theme) {
        UIDialog dialog = new UIDialog(context, theme);
        dialog.setViewPartVisitor(this);
        return dialog;
    }

}
